﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBD_DAL.Entities;
using BBD_DAL.DBConnector;
using System.Data;
using MySql.Data.MySqlClient;

namespace BBD_DAL.DAO
{
	public class SchoolDao: IBaseDao<School>
	{

		private DBConnection dbConnector;
		private Dictionary<string, string> queries;

		public SchoolDao(string connectionString)
		{
			this.Initialize(connectionString);
		}

		public void Initialize(string connectionString)
		{
			this.dbConnector = new DBConnection(connectionString);
			queries = new Dictionary<string, string>();
			queries.Add("Get", "SELECT * FROM szkoly WHERE szkola_id = @id;");
			queries.Add("GetByCity", "SELECT * FROM szkoly WHERE miejscowosc_id = @id;");
			
			queries.Add("Save", "UPDATE szkoly SET szkola_nazwa = @name, data = @date, miejscowosc_id = @mid WHERE szkola_id = @id;");
			queries.Add("Insert", "INSERT INTO szkoly (szkola_nazwa, miejscowosc_id, data) VALUES (@name, @mid, @date);");
			queries.Add("Delete", "DELETE FROM szkoly WHERE id = @id;");
			queries.Add("DeleteAll", "DELETE FROM szkoly; ALTER TABLE szkoly AUTO_INCREMENT = 1;");
			queries.Add("List", "SELECT * FROM szkoly;");
		}

		public School Get(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["Get"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<School> schools = this.Convert(data);

			return schools != null ? schools[0] : null;
		}

		public List< School >GetByCity(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["GetByCity"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<School> schools = this.Convert(data);

			return schools != null ? schools : null;
		}

		public void Save(School entity)
		{
			this.dbConnector.RunQuery(this.queries["Save"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = entity.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@name", Value = entity.SchoolName, MySqlDbType = MySqlDbType.Text},
				new MySqlParameter(){ParameterName = "@mid", Value = entity.City.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@date", Value = entity.Date, MySqlDbType = MySqlDbType.DateTime}
			});
		}

		public School Insert(School entity)
		{
			long id;
			this.dbConnector.RunQuery(this.queries["Insert"], new MySqlParameter[]{				
				new MySqlParameter(){ParameterName = "@name", Value = entity.SchoolName, MySqlDbType = MySqlDbType.Text},
				new MySqlParameter(){ParameterName = "@mid", Value = entity.City.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@date", Value = entity.Date, MySqlDbType = MySqlDbType.DateTime}
			}, out id);

			entity.Id = id;

			return entity;
		}

		public void Delete(long id)
		{
			this.dbConnector.RunQuery(this.queries["Delete"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public void Delete(School entity)
		{
			this.Delete(entity.Id);
		}

		public void Delete()
		{
			this.dbConnector.RunQuery(this.queries["DeleteAll"], null);
		}

		public List<School> List()
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["List"], null, out data);
			return this.Convert(data);
		}

		private List<School> Convert(DataTable data)
		{
			if (data == null || data.Rows == null || data.Rows.Count == 0)
				return null;

			List<School> schools = new List<School>();
			foreach (DataRow row in data.Rows)
			{
				School school = new School();
				school.Id = Int64.Parse(row["szkola_id"].ToString());
				school.SchoolName = row["szkola_nazwa"].ToString();
				school.Date = (DateTime) row["data"];
				school.City = new City() { Id = Int64.Parse(row["miejscowosc_id"].ToString()) };
				schools.Add(school);
			}
			return schools;
		}
	}
}
